<?php
class LZ_Hot
{
	var $id = 'hot_id';
	var $name = 'name';
	var $count = 'count';
	var $day = 'day';
	
	var $table = 'hot';
	
	function LZ_Hot()
	{
		$this->table = LZ_MYSQL_PREFIX.$this->table;
	}
	
	function today()
	{
		return intval(time()/(24*60*60));
	}
	
	function add($name)
	{
		global $db;
		$name = trim($name);
		if (!$name) return;
		$this->delete_old();
		$day = $this->today();
		$sql = "UPDATE $this->table SET `count`=`count`+1 WHERE `name`='$name' AND day=$day";
		$db->query($sql);
		if ($db->affected_rows() <= 0)
		{
			$sql = "SELECT * FROM `".LZ_MYSQL_PREFIX."photo` WHERE `tag` LIKE '%$name%'";
			$r = $db->query($sql);
			if ($db->num_rows($r) > 0)
			{
				$sql = make_insert_sql($this->table,array('name'=>$name,'count'=>1,'day'=>$this->today()));
				return $db->query($sql);
			}
		}
	}
	
	function change_keyword_name($name1,$name2)
	{
		global $db;
		$sql = make_update_sql($this->table,array('name'=>$name2),array('name'=>$name1));
		return $db->query($sql);
	}
	
	function delete_all()
	{
		global $db;
		$sql = "DELETE FROM $this->table";
		return $db->query($sql);
	}
	
	function delete_old()
	{
		global $db,$config;
		$last_days = $config['hot_keyword_count_day']?$config['hot_keyword_count_day']:30;
		$day = $this->today() - $last_days;
		$sql = "DELETE FROM $this->table WHERE day<$day";
		return $db->query($sql);
	}
	
	function delete($name)
	{
		global $db;
		$sql = "DELETE FROM $this->table WHERE `name` = '$name';";
		return $db->query($sql);
	}
	
	function get_list($num = 10)
	{
		global $db;
		$sql = "SELECT name,SUM(count) as count FROM $this->table WHERE 1 GROUP BY name ORDER BY count DESC LIMIT $num";
		return $db->get_all($sql);
	}
}
?>